
#include <utils/cameraprojectorsetup.h>


ceCameraProjectorSetup::ceCameraProjectorSetup()
{
}


void ceCameraProjectorSetup::SetupPerspective(const ceVector3f &eye, const ceVector3f& up, const ceBoundingBox &bbox, ceCamera &camera, ceProjector &projector, ceVector2f& size)
{
  camera.SetEye(eye);
  camera.SetUp(up);
  camera.SetSpot(bbox.GetCenter());
  camera.FinishTransformation();

  ceBoundingBox tmp;
  tmp.Add(camera.GetMatrix(), bbox);
  tmp.Update();

  const ceVector3f& cen = tmp.GetCenter();
  const ceVector3f& max = tmp.GetMax();
  float near = 1.0f;
  float far = 1024.0f;

  float alpha = near / max.z;
  float r = max.x * alpha;
  float t = max.y * alpha;
  projector.SetPerspective();
  projector.SetLeft(r);
  projector.SetRight(-r);
  projector.SetBottom(t);
  projector.SetTop(-t);
  projector.SetNear(near);
  projector.SetFar(far);
  projector.FinishTransformation();

  float beta = cen.z / max.z;


  size = ceVector2f(beta * max.x, beta * max.y);
}



void ceCameraProjectorSetup::SetupPerspective(const ceVector3f &eye, const ceVector3f& up, const ceSphere& sphere, ceCamera &camera, ceProjector &projector, float zNear, float zFar)
{
  camera.SetEye(eye);
  camera.SetUp(up);
  camera.SetSpot(sphere.GetCenter());
  camera.FinishTransformation();

	ceVector3f p = camera.GetMatrix () * sphere.GetCenter ();

	float r = zNear * sphere.GetRadius() / p.z;
  projector.SetPerspective();
  projector.SetLeft(-r);
  projector.SetRight(r);
  projector.SetBottom(-r);
  projector.SetTop(r);
  projector.SetNear(zNear);
  projector.SetFar(zFar);
  projector.FinishTransformation();
}
